#       FeedEntryParser_test.py
#       
#       Copyright 2008 Joshua Scotton <josh@joshuascotton.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Joshua Scotton"
__author__ = "Joshua Scotton <josh@joshuascotton.com>"

import unittest
import sys
import os
import ConfigParser
import logging

from ConfigParser import ParsingError

try:
 	import pygtk
  	pygtk.require("2.4")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)

sys.path += [os.path.abspath('..')]

from utils.feed_utils import FeedEntryParser

class FeedEntryParserTest(unittest.TestCase):

    def setUp(self,debug=False):
        """Sets up everything for the test"""
        self.debug = debug
        if(self.debug):
            #set up logger
            self.logger = logging.getLogger('Entertainer')
            fileHandle = logging.FileHandler('./tests.log')
            fileHandle.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(funcName)s %(message)s'))
            self.logger.addHandler(fileHandle)
            self.logger.setLevel(logging.DEBUG)
        
        #test strings
        self.testString001 = "<body><b>Hello World</body></b>"
        self.testString002 = "<body><span id=\"001\"><u><b>&nbsp;He<i>l</i>lo <big>World</b></big><s>!</s><sub>!</sub><div><sup>!</sup><small>!</small><tt></div>...</tt></u></span></body>"
        self.resultString001 = "Hello World"
        self.resultString002 = "<span id=\"001\"><u><b>&nbsp;He<i>l</i>lo <big>World</b></big><s>!</s><sub>!</sub><sup>!</sup><small>!</small><tt>...</tt></u></span>"
        self.resultString003 = "<body>Hello World</body>"
        self.resultString004 = "<span id=\"001\"><u><b> He<i>l</i>lo <big>World</b></big><s>!</s><sub>!</sub><sup>!</sup><small>!</small><tt>...</tt></u></span>"
        
    def testStripTags001(self):
        """testStripTags001: tests if FeedEntryParser strips all tags from input using default tags"""
        output = FeedEntryParser().strip_tags(self.testString001)
        if(self.debug):
            self.logger.debug("EXPECTED: " + self.resultString001)
            self.logger.debug("OUTPUT: " + output)
        self.assertEqual(output,self.resultString001)

    def testStripTags002(self):
        """testStripTags002: tests if FeedEntryParser strips all tags from input using non-default tags"""
        output = FeedEntryParser().strip_tags(self.testString001, ["body"])
        if(self.debug):
            self.logger.debug("EXPECTED: " + self.resultString003)
            self.logger.debug("OUTPUT: " + output)
        self.assertEqual(output,self.resultString003)

    def testStripNonPangoTags(self):
        """testStripNonPangoTags: tests if FeedEntryParser strips all non-pango tags from input"""
        output = FeedEntryParser().strip_non_pango_tags(self.testString002)
        if(self.debug):
            self.logger.debug("EXPECTED: " + self.resultString002)
            self.logger.debug("OUTPUT: " + output)
        self.assertEqual(output,self.resultString002)
        
    def testConvert(self):
        """testConvert: tests if FeedEntryParser converts input to correct output"""
        output = FeedEntryParser().convert(self.testString002)
        if(self.debug):
            self.logger.debug("EXPECTED: " + self.resultString004)
            self.logger.debug("OUTPUT: " + output)
        self.assertEqual(output,self.resultString004)
        
